#!/bin/bash

# Name of your app.
APP="Ethereum-Wallet"
# The path of you app to sign.
APP_PATH="dist_wallet/Ethereum-Wallet-macosx64-0-4-0/Ethereum-Wallet.app"
# The path to the location you want to put the signed package.
RESULT_PATH="dist_wallet/$APP.pkg"
# The name of certificates you requested.
APP_KEY="3rd Party Mac Developer Application: Stiftung Ethereum (3W6577R383)"
#"Mac Developer: Fabian Vogelsteller (YK2P3ZENMX)"
#"3rd Party Mac Developer Application: Stiftung Ethereum (3W6577R383)"
INSTALLER_KEY="3rd Party Mac Developer Installer: Stiftung Ethereum (3W6577R383)"

cd $APP_PATH && find . -name "*.DS_Store" -type f -delete
cd ../../../

FRAMEWORKS_PATH="$APP_PATH/Contents/Frameworks"

codesign --deep -fs "$APP_KEY" --entitlements child.plist "$FRAMEWORKS_PATH/Electron Framework.framework/Versions/A"
codesign --deep -fs "$APP_KEY" --entitlements child.plist "$FRAMEWORKS_PATH/$APP Helper.app/"
codesign --deep -fs "$APP_KEY" --entitlements child.plist "$FRAMEWORKS_PATH/$APP Helper EH.app/"
codesign --deep -fs "$APP_KEY" --entitlements child.plist "$FRAMEWORKS_PATH/$APP Helper NP.app/"
if [ -d "$FRAMEWORKS_PATH/Squirrel.framework/Versions/A" ]; then
  # Signing a non-MAS build.
  codesign --deep -fs "$APP_KEY" --entitlements child.plist "$FRAMEWORKS_PATH/Mantle.framework/Versions/A"
  codesign --deep -fs "$APP_KEY" --entitlements child.plist "$FRAMEWORKS_PATH/ReactiveCocoa.framework/Versions/A"
  codesign --deep -fs "$APP_KEY" --entitlements child.plist "$FRAMEWORKS_PATH/Squirrel.framework/Versions/A"
fi

codesign --deep -fs "$APP_KEY" --entitlements nodes.plist "$FRAMEWORKS_PATH/node/eth/eth"
codesign --deep -fs "$APP_KEY" --entitlements nodes.plist "$FRAMEWORKS_PATH/node/geth/geth"


codesign -fs "$APP_KEY" --entitlements parent.plist "$APP_PATH"

# codesign --force --verify --verbose --sign "$APP_KEY" "$APP_PATH"

#productbuild --component "$APP_PATH" /Applications --sign "$INSTALLER_KEY" "$RESULT_PATH"


echo "################"
echo "### verifing app"
codesign --verify -vvvv "$RESULT_PATH"
spctl -a -vvvv "$RESULT_PATH"
